package utils

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/labstack/echo/v4"
)

type ResultParam struct {
	Ret  int64       `json:"ret"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

const (
	//成功
	SuccessCode = 200
	//错误
	ErrorCode = 400
	//非法用户
	IllegalUserCode = 401
	//签名验证失败
	SignValidFailCode = 402
	//交易哈希错误
	ErrorTxHashCode = 403
	//数据为空
	NullDataCode = 404
	//参数类型错误
	ParamTypeErrCode = 405
)

const (
	SignValidFailResult = "签名验证失败"
	IllegalUserResut    = "非法用户"
	ErrParamTypeResult  = "参数类型错误"
	ErrorTxHashResut    = "交易哈希错误"
	GetParsFailResult   = "获取参数失败"
	ParsFailResult      = "参数校验失败"
	GetDataNullResult   = "数据不存在"
	GetDataFailResult   = "获取数据失败"
	AddSuccessResult    = "添加成功"
	UpdateSuccessResult = "修改成功"
	DeleteSuccessResult = "删除成功"
	AddFailResult       = "添加失败"
	UpdateFailResult    = "修改失败"
	DeleteFailResult    = "删除失败"
	GetFailResult       = "获取失败"
)

func ToResultParam(b []byte) ResultParam {
	var rp ResultParam
	err := json.Unmarshal(b[:], &rp)
	if err != nil {
		println(err.Error())
		return ResultParam{}
	}
	return rp
}

func SuccessRespone(c echo.Context, data string) error {
	return c.HTML(http.StatusOK, data)
}

func Success(c echo.Context, msg string, data interface{}) error {
	return Result(c, SuccessCode, msg, data)
}

func SuccessNullMsg(c echo.Context, data interface{}) error {
	return Result(c, SuccessCode, "", data)
}
func SuccessNull(c echo.Context, msg string) error {
	return Result(c, SuccessCode, msg, nil)
}

func Error(c echo.Context, msg string, data interface{}) error {
	return Result(c, ErrorCode, msg, data)
}

func ErrorNull(c echo.Context, msg string) error {
	return Result(c, ErrorCode, msg, nil)
}

func NullData(c echo.Context) error {
	return Result(c, NullDataCode, "暂无数据", nil)
}

func ErrorTxHash(c echo.Context) error {
	return Result(c, ErrorTxHashCode, ErrorTxHashResut, nil)
}

func IllegalUser(c echo.Context) error {
	return Result(c, IllegalUserCode, IllegalUserResut, nil)
}

func ErrParamType(c echo.Context) error {
	return Result(c, ParamTypeErrCode, ErrParamTypeResult, nil)
}

func SignValidFail(c echo.Context) error {
	return Result(c, SignValidFailCode, SignValidFailResult, nil)
}

func ResultApi(c echo.Context, data interface{}) error {
	return c.JSON(http.StatusOK, data)
}

func Result(c echo.Context, ret int64, msg string, data interface{}) error {
	resultMap := map[string]interface{}{
		"ret":  ret,
		"msg":  msg,
		"data": data,
	}
	return c.JSON(http.StatusOK, resultMap)
}

func Alert(c echo.Context, tip string) error {
	return RedirectAndAlert(c, tip, "")
}

func RedirectAndAlert(c echo.Context, tip, url string) error {
	var js string
	if tip != "" {
		js += fmt.Sprintf("alert('%v');", tip)
	}
	js += fmt.Sprintf("parent.location.href = '%v';", url)
	return ResultHtml(c, fmt.Sprintf("<script>%v</script>", js))
}

func Redirect(c echo.Context, url string) error {
	return c.Redirect(http.StatusMovedPermanently, url)
}

func ResultHtml(c echo.Context, html string) error {
	return c.HTML(http.StatusOK, html)
}

func ResultString(c echo.Context, str string) error {
	return c.String(http.StatusOK, str)
}
